<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <style>
    .layui-card-body {
      height: 753px;
    }
  </style>
</head>

<body>
  <div class="layui-card">
    <div class="layui-card-header">
      <span class="layui-breadcrumb">
        <a href="">项目管理</a>
        <a><cite>添加项目</cite></cite></a>
      </span>
    </div>
    <div class="layui-card-body">
      <form class="layui-form" action="" style="width: 800px;height: 400px;">
        <!-- 图片名称： -->
        <div class="layui-form-item">
          <label class="layui-form-label">所属分类：</label>
          <div class="layui-input-block">
            <select name="pc_id" lay-verify="">
              <option value="">请选择项目分类</option>
              <% category.forEach((item)=>{%>
                <option value="<%=item.pc_id%>">
                  <%=item.pc_name%>
                </option>
                <%}) %>

            </select>
          </div>
        </div>
        <!-- 图片名称： -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目名称：</label>
          <div class="layui-input-block">
            <input type="text" name="project_title" required lay-verify="required" placeholder="请输入项目名称"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <!-- 上传图片 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目图片：</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="thumb">
              <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-input-block">
            <blockquote class="layui-elem-quote">预览图
              <div>
                <img src="" alt="" class="thumb" style="width:200px;">

              </div>
              <input type="hidden" name="project_thumb">
            </blockquote>
          </div>
        </div>
        <!-- 项目描述 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目描述：</label>
          <div class="layui-input-block">
            <textarea style="display: none;" id="demo" name="project_content"></textarea>
          </div>
        </div>
        <!-- 项目组图 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目组图：</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="pthumb">
              <i class="layui-icon">&#xe67c;</i>上传项目组图
            </button>
          </div>
        </div>
        <div class="layui-form-item">

          <div class="layui-input-block">
            <blockquote class="layui-elem-quote">预览图
              <div class="imglist"></div>
              <!-- 隐藏域 -->
              <input type="hidden" name="project_banner">
            </blockquote>
          </div>
        </div>
        <!-- 提交按钮 -->
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="projectform">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</body>

</html>

<script src="/layui/layui.js"></script>
<script>
  layui.use(['element', 'upload', 'jquery', 'layer', 'form', 'layedit'], function () {
    var { element, upload, $, layer, form, layedit } = layui
    // js方式
    // var thumb = document.querySelector(".thumb")
    // var project_thumb = document.querySelector("[name = project_thumb]")

    //缩略图上传
    upload.render({
      elem: "#thumb",
      url: "/admin/project/upload",
      done: (res) => {
        if (res.code == 1) {
          $(".thumb").attr("src", res.url)
          $("[name = project_thumb]").val(res.url)
        } else {
          layer.msg("图片上传失败")
        }

      }
    })


    //富文本编辑器
    var layeditIns = layedit.build('demo', {

    }); //建立编辑器

    //上传组图
    var imglist = document.querySelector(".imglist")
    var pthumbList = []
    //组图上传
    upload.render({
      elem: "#pthumb",
      url: "/admin/project/upload",
      multiple: true,
      number: 4,
      done: (res) => {
        if (res.code == 1) {
          //四张图片进行渲染

          //原生js
          //创建元素节点
          // var elem = document.createElement("img")
          // elem.style.width = "200px"
          // elem.src = res.url
          // // elem.setAttribute("src", res.url)  这个是设置属性的class
          // imglist.appendChild(elem)


          //jquery的方法
          $("<img width='200'>").attr("src", res.url).appendTo($(".imglist"))
          pthumbList.push(res.url)
          $("[name=project_banner]").val(pthumbList.join(","))
        }
        // console.log(res);
      }

    })


    form.on("submit(projectform)", data => {
      // console.log(layedit.getContent(layeditIns));
      data.field.project_content = layedit.getContent(layeditIns)
      // console.log(data.field);
      $.ajax({
        type: "POST",
        url: "/admin/project/insert",
        data: data.field,
        success: (res) => {
          var { code, msg } = res
          // console.log(res);
          // console.log(layer);
          layer.msg(msg)
        }
      })
      return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })

  })
</script>